#include "CtrlMsg.h"
#include "Qslog.h"

CtrlMsg::CtrlMsg(QWidget *parent) : QWidget(parent) {
  ui.setupUi(this);
  init();
}

CtrlMsg::~CtrlMsg() { 
  QsLogging::Logger::destroyInstance(); 
}

void CtrlMsg::init() { init_log(); }

void CtrlMsg::init_log() {
  QsLogging::Logger &logger = QsLogging::Logger::instance();
  logger.setLoggingLevel(QsLogging::TraceLevel);
  QsLogging::DestinationPtr objectDestination(
      QsLogging::DestinationFactory::MakeFunctorDestination(
          this, SLOT(logging(QString, int))));
  logger.addDestination(objectDestination);
}

void CtrlMsg::logging(QString strMsg, int level) {
  QListWidgetItem *item = new QListWidgetItem(strMsg, ui.listWidgetMsg);
  ui.listWidgetMsg->addItem(item);
  ui.listWidgetMsg->setCurrentRow(ui.listWidgetMsg->count() - 1);
}

void CtrlMsg::on_pbClear_clicked() { ui.listWidgetMsg->clear(); }